<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Report_m extends CI_Model {
	function __construct()
    {
        parent::__construct();
    }
	
	/******通过日期获取订单量*******
	** return array;
	**************************/
	function getOrderNum($orderDate,$whereshop='',$end_date='')
	{
		if($whereshop)$where=" AND shopname in('".$whereshop."')";
        $date_where=$end_date!=''?"dates between '".$orderDate."' and '".$end_date."'":"dates='".$orderDate."'";
		$num =$this->fun->getdatabasesdetail('db_order_list'," AND {$date_where} and order_status !=6 AND order_status !=7 $where"," count(*) as Num");
		
        return $num['Num'];
        
	}
    
    public function getSalesByMonth($orderDate,$wherestr='',$end_date='',$column='province',$orderbycolnum='goods_amount_total'){
        $date_where=$end_date!=''?"dates between '".$orderDate."' and '".$end_date."'":"dates='".$orderDate."'";
        $order_list =  $this->fun->getdatabaseslist("db_order_list B","$wherestr and  {$date_where} AND
         order_status !=6 AND order_status !=7 group by substring(B.dates,1,7) order by $orderbycolnum desc","*,substring(dates,1,7) as month,count(*) as order_num,sum(B.shipping_fee)
          as shipping_fee_total,SUM(B.shipping_expectfee) as realexpress_fre,SUM(B.goods_amount) as goods_amount_total,sum(platform_fee) as platform_fee_total,
          sum(pay_fee) as payfee_total");
        //echo $this->db->last_query();
        $costpriceData=$this->fun->getdatabaseslist("db_order_list B left join db_order_sell A on A.order_id=B.order_id","$wherestr and  {$date_where} AND
         order_status !=6 AND order_status !=7 group by substring(B.dates,1,7)","substring(B.dates,1,7) as month,sum(goods_number) 
        as sell_total,sum(costprice*goods_number) as costprice_total,A.order_id,goods_id,B.dates");
        foreach($order_list as $key=>$orderinfo){
            foreach($costpriceData as $index=>$sourceprice){
                if($orderinfo['month']==$sourceprice['month']){
                    $order_list[$key]['costprice_total']=$sourceprice['costprice_total'];
                    $order_list[$key]['sell_total']=$sourceprice['sell_total'];   
                }
            }
        }
        return $order_list;
    }

	function getOrderAmount($orderDate,$whereshop='',$end_date='')
	{
		if($whereshop)$where=" AND shopname in('".$whereshop."')";
        $date_where=$end_date!=''?"dates between '".$orderDate."' and '".$end_date."'":"dates='".$orderDate."'";
		$order = $this->fun->getdatabasesdetail('db_order_list'," AND {$date_where} AND order_status !=6 AND order_status !=7 $where",' SUM(goods_amount) as Num');
		return $order['Num'];
	}
    //按地区统计销售金额 按国家统计销售金额
    function getOrderAmountList($orderDate,$wherestr='',$end_date='',$column='province',$orderbycolnum='goods_amount_total'){
        $date_where=$end_date!=''?"dates between '".$orderDate."' and '".$end_date."'":"dates='".$orderDate."'";
        $order_list =  $this->fun->getdatabaseslist("db_order_list B","$wherestr and  {$date_where} AND
         order_status !=6 AND order_status !=7 group by $column order by $orderbycolnum desc","*,count(*) as order_num,sum(B.shipping_fee)
          as shipping_fee_total,SUM(B.shipping_expectfee) as realexpress_fre,SUM(B.goods_amount) as goods_amount_total,sum(platform_fee) as platform_fee_total,
          sum(pay_fee) as payfee_total");
        //echo $this->db->last_query();
        $costpriceData=$this->fun->getdatabaseslist("db_order_list B left join db_order_sell A on A.order_id=B.order_id","$wherestr and  {$date_where} AND
         order_status !=6 AND order_status !=7 group by B.$column","sum(goods_number) 
        as sell_total,sum(costprice*goods_number) as costprice_total,A.order_id,goods_id,B.$column");
        foreach($order_list as $key=>$orderinfo){
            foreach($costpriceData as $index=>$sourceprice){
                if($orderinfo[$column]==$sourceprice[$column]){
                    $order_list[$key]['costprice_total']=$sourceprice['costprice_total'];
                    $order_list[$key]['sell_total']=$sourceprice['sell_total'];   
                }
                $shop_names[]=  $orderinfo['shopname'];  
            }
        }
        return $order_list;
    }
    
    
	function getOrdershippingfee($orderDate,$whereshop='',$end_date='')
	{
		if($whereshop)$where=" AND shopname in('".$whereshop."')";
        $date_where=$end_date!=''?"dates between '".$orderDate."' and '".$end_date."'":"dates='".$orderDate."'";
		$order = $this->fun->getdatabasesdetail('db_order_list'," AND {$date_where} AND order_status !=6 AND order_status !=7 $where",' SUM(shipping_fee) as Num');
		return $order['Num'];
	}
	function getSellgoodsprice($orderDate,$whereshop='',$end_date='')
	{
		$cosprice =$goodsnNum=0;
		if($whereshop)$where=" AND shopname in('".$whereshop."')";
        $date_where=$end_date!=''?"dates between '".$orderDate."' and '".$end_date."'":"dates='".$orderDate."'";
		$order = $this->fun->getdatabaseslist('db_order_list'," AND {$date_where} AND order_status !=6 AND order_status !=7 $where",' order_id');
		foreach($order as $key=>$orderID)
		{
			$soncosprice =$songoodsnNum=0;
			$order_id = $orderID['order_id'];
			$price = $this->db->select('costprice,goods_number')->get_where('db_order_sell',"  order_id={$order_id}")->result_array();
			foreach($price as $skey=>$res)
			{
				$songoodsnNum = $songoodsnNum+$res['goods_number'];
				$soncosprice  = $soncosprice + $res['goods_number']* $res['costprice'];
			}
			$cosprice = $cosprice+$soncosprice;
			$goodsnNum = $goodsnNum +$songoodsnNum;
			
		}

		return array("cosprice"=>$cosprice,"goodsnNum"=>$goodsnNum);
	}
    
    function newProductSalesList($starttime,$endtime,$wherestr,$limitdata="50",$by_clumn='sell_total',$factory_id='',$create=''){
        $starttimelimit=strtotime("$starttime 00:00:00");
        $endtimelimit=strtotime("$endtime 23:59:59");
        if($factory_id)$fatorywhere=" and supplier in('$factory_id')";
		if($create)$createwhere=" and createName like '".$create."'";
        
		 $goods_sku=$this->input->get("goods_sku");
        if($goods_sku!=''){
            $wherestrs=" and sku in('$goods_sku')";
        }
        $new_goodslist = $this->fun->getdatabaseslist("db_product_develop","$fatorywhere $createwhere and overtime >=$starttimelimit AND overtime <= '$endtimelimit' $wherestrs","sku");
      //echo $this->db->last_query();
        $goods_skus=iarray_column($new_goodslist,'sku');
        $goods_sku_str=implode("','",$goods_skus);
        $saleslist=array();
        if(count($goods_skus)>0){
			//需要统计五位关联
			$skulist = $this->fun->getdatabasesdetail('db_goods'," and goods_parent_sku in('$goods_sku_str')"," GROUP_CONCAT(goods_sku) as sku");
			if($skulist["sku"])
			{
				$newgoods_sku_str = explode(",",$skulist["sku"]);
				$newgoods_sku_str=implode("','",$newgoods_sku_str);
			}
            
            $wherestr.=" and A.goods_sn in('$newgoods_sku_str')";
            $saleslist=$this->fun->getdatabaseslist("db_order_sell A left join db_order_list B on A.order_id=B.order_id
             left join (select count(*) as goods_total,order_id from db_order_sell group by order_id) as C on C.order_id=B.order_id","$wherestr and 
             order_status not in(6,7) group by A.goods_id order by $by_clumn desc limit $limitdata",
            "*,SUM(B.goods_amount) as amount_total,sum(A.goods_number) as sell_total,sum(B.shipping_fee)/goods_total
            as shipping_fee_total,sum(shipping_expectfee)/goods_total as shipping_expectfee_total,
            SUM(A.goods_amount) as goods_amount_total,sum(platform_fee)/goods_total as platform_fee_total,
            sum(pay_fee)/goods_total as payfee_total,sum(costprice) as costprice_total");
            //echo $this->db->last_query();
        }
            $data_total=$this->db->query("select  COUNT(DISTINCT A.goods_id) as datatotal  FROM db_order_sell A 
            left join db_goods on A.goods_id=db_goods.goods_id
            left join db_order_list B on A.order_id=B.order_id  WHERE 1 $wherestr and
            order_status not in(6,7)")->result_array();
            //echo $this->db->last_query();
            $returndata=array('list'=>$saleslist,'total'=>$data_total[0]['datatotal']);
        return $returndata;
    }
    
    //热销商品列表
    function getSaleGoodsRank($orderDate,$wherestr='',$end_date='',$rownum=20,$by_clumn='sell_total'){
        $wherestr.=" and A.goods_id>0";
        $date_where=$end_date!=''?"ordertime between '".$orderDate."' and '".$end_date."'":"ordertime='".$orderDate."'";       //left join db_goods on A.goods_id=db_goods.goods_id    
            $order_list =  $this->fun->getdatabaseslist("db_order_sell A left join db_order_list B on A.order_id=B.order_id
             left join db_goods on A.goods_id=db_goods.goods_id
             left join (select count(*) as goods_total,order_id from db_order_sell group by order_id) as C on C.order_id=B.order_id","$wherestr and  {$date_where} AND
            order_status not in(6,7) group by A.goods_id order by $by_clumn desc limit $rownum",
            "*,SUM(B.goods_amount) as amount_total,sum(A.goods_number) as sell_total,sum(B.shipping_fee)/goods_total
            as shipping_fee_total,sum(shipping_expectfee)/goods_total as shipping_expectfee_total,
            SUM(A.goods_amount) as goods_amount_total,sum(platform_fee)/goods_total as platform_fee_total,
            sum(pay_fee)/goods_total as payfee_total,sum(costprice) as costprice_total");
            
            $data_total=$this->db->query("select  COUNT(DISTINCT A.goods_id) as datatotal  FROM db_order_sell A 
            left join db_goods on A.goods_id=db_goods.goods_id
            left join db_order_list B on A.order_id=B.order_id  WHERE 1 $wherestr and  {$date_where} AND
            order_status not in(6,7)")->result_array();
            $returndata=array('list'=>$order_list,'total'=>$data_total[0]['datatotal']);
        return $returndata;
    }
    
    //热销商品列表
    function getFbaGoodsRank($orderDate,$wherestr='',$end_date='',$rownum=30,$by_clumn='sell_total'){
        $wherestr.=" and A.goods_id>0";
        $date_where=$end_date!=''?"dates between '".$orderDate."' and '".$end_date."'":"dates='".$orderDate."'";          
            $order_list =  $this->fun->getdatabaseslist("db_amzordertmpsell A left join db_amzordertmp B on A.ordertmpid=B.id","$wherestr and  {$date_where} AND
            status =1 group by goods_id order by $by_clumn desc limit $rownum",
            "*,sum(A.ordernum) as sell_total,sum(A.goods_amount/B.goods_amount*B.shipping_fee)
            as shipping_fee_total,SUM(A.goods_amount) as goods_amount_total,sum(platform_fee) as platform_fee_total,
            sum(A.goods_amount/B.goods_amount*payship_fee) as payship_fee_total,sum(pay_fee) as payfee_total,sum(costprice) as costprice_total");
            $goods_arr=array();
            
/*$order_list =  $this->fun->getdatabaseslist("db_amzordertmpsell A left join db_amzordertmp B on A.ordertmpid=B.id","$wherestr and  {$date_where} AND
            status =1 group by goods_id order by $by_clumn desc limit $rownum",
            "*,sum(A.ordernum) as sell_total,sum(B.shipping_fee)
            as shipping_fee_total,SUM(B.goods_amount) as goods_amount_total,sum(platform_fee) as platform_fee_total,
            sum(payship_fee) as payship_fee_total,sum(pay_fee) as payfee_total,sum(costprice) as costprice_total");*/

            $data_total=$this->db->query("select  COUNT(DISTINCT goods_id) as datatotal  FROM db_amzordertmpsell A left 
            join db_amzordertmp B on A.ordertmpid=B.id  WHERE 1 $wherestr and  {$date_where} AND
            status =1")->result_array();
            
        
            //get_Goods_costprice
            foreach($order_list as $key=>$keyval){
                $goods_arr[]=$keyval['goods_id'];
            }

         /*if(count($goods_arr)>0){
            $goods_arr_str=implode(",",$goods_arr);
            $order_mount_list =  $this->fun->getdatabaseslist("db_amzordertmp B,db_amzordertmpsell A","$wherestr and  {$date_where} &&
             A.ordertmpid=B.id && goods_id in($goods_arr_str) &&
                status=1 group by goods_id",
             "*,SUM(B.goods_amount) as amount_total,sum(B.shipping_fee)
              as shipping_fee_total,sum(platform_fee) as platform_fee_total,
              sum(pay_fee) as payfee_total");
             $new_total_list=array();
            foreach($order_mount_list as $key=>$keyval){
                $new_total_list[$keyval['goods_id']]=$keyval;
            }
        }*/
        

        $skus=iarray_column($order_list,'productid');
        $skustrs=implode("','",$skus);
        $first_ship_fre=$this->fun->getdatabaseslist("t_overseaproduct"," and sku in('$skustrs')","deliverprice,sku");

        $goods_firstship_fre=iarray_column($first_ship_fre,'deliverprice','sku');
        foreach($order_list as $key=>$keyval){
            $keyval['productid']=strtoupper($keyval['productid']);
            $order_list[$key]['amount_total']=$new_total_list[$keyval['goods_id']]['amount_total'];
            $order_list[$key]['platform_fee_total']=$new_total_list[$keyval['goods_id']]['platform_fee_total'];
            $order_list[$key]['firstshipment_fre']=$goods_firstship_fre[$keyval['productid']]*$keyval['sell_total'];
            //$order_list[$key]['shipping_fee_total']=$new_total_list[$keyval['goods_id']]['shipping_fee_total'];
            $order_list[$key]['payship_fee_total']=round($order_list[$key]['payship_fee_total'],2);
            $order_list[$key]['goods_amount_total']+=$order_list[$key]['shipping_fee_total'];
            $order_list[$key]['goods_amount_total']=round($order_list[$key]['goods_amount_total'],2);
        }  
        
        $returndata=array('list'=>$order_list,'total'=>$data_total[0]['datatotal']);
        return $returndata;  
    }
    
    
    //返回商品实际运费
    function getOrderExpressPirce($orderDate,$wherestr='',$end_date=''){
        $date_where=$end_date!=''?"dates between '".$orderDate."' and '".$end_date."'":"dates='".$orderDate."'";
        $order_list =  $this->fun->getdatabaseslist("db_order_sell A left join db_order_list B on A.order_id=B.order_id","$wherestr and  {$date_where} AND
         order_status !=6 AND order_status !=7");
		 if($_COOKIE["userid"] ==295)
		 {
			//echo $this->db->last_query();
			//echo "<br>";
		 }
         $express_prices=array();

		foreach($order_list as $skey=>$res){
			if($res['shipping_realfee']>0)
			{
				$realfee=$res['shipping_realfee'];
			}else{
				$realfee=$res['shipping_expectfee'];
			}
            $express_prices[$res['order_id']]+=$realfee;
		}
        return $express_prices;
    }
    
    //单商品销售数据查询
    function getGoodSaleInfo($orderDate,$wherestr='',$end_date=''){
        $date_where=$end_date!=''?"dates between '".$orderDate."' and '".$end_date."'":"dates='".$orderDate."'";
        $order_list =  $this->fun->getdatabaseslist("db_order_sell A left join db_order_list B on A.order_id=B.order_id","$wherestr and  {$date_where} AND
         order_status !=6 AND order_status !=7 group by dates","*,SUM(B.goods_amount) as amount_total,sum(A.goods_number) as sell_total,sum(B.shipping_fee)
          as shipping_fee_total,sum(pack_fee) as pack_fee_total,SUM(A.goods_amount) as goods_amount_total,sum(platform_fee) as platform_fee_total,
          sum(pay_fee) as payfee_total,sum(A.costprice * A.goods_number) as costprice_total,count(*) as order_total,sum(goods_number) as goods_number_total");
         $express_prices=array();
        // dump($order_list);
		foreach($order_list as $skey=>$res){
			if($res['shipping_realfee']>0)
			{
				$order_list[$skey]['realfee']= $res['shipping_realfee'];
			}else{
				$order_list[$skey]['realfee'] = $res['shipping_expectfee'];
			}
		}
        return $order_list;
    }
    
    public function getFBAorders($orderDate='',$wherestr='',$end_date='',$page=""){
        $pageSize=10000;
        if ($page !='') {
            $offset = ($page - 1) * $pageSize;
            $limit = " LIMIT $pageSize OFFSET $offset";
        }
        if($orderDate!='' or $end_date!=''){
            $date_where=$end_date!=''?"and dates between '".$orderDate."' and '".$end_date."'":"and dates='".$orderDate."'";
        }
        $date_where.=" and status=1";
        $sqlCount ="SELECT COUNT(*) as total FROM (SELECT sum(B.shipping_fee) FROM db_amzordertmp B
         left join db_amzordertmpsell A on A.ordertmpid=B.id where 1 $wherestr  {$date_where} group by dates) AA";
        $allCount = $this->db->get_one($sqlCount);
        $ret['total']=$allCount['total'];
        $order_list =  $this->fun->getdatabaseslist("db_amzordertmp B left join (select sum(costprice) as costprice_total,ordertmpid,
        sum(ordernum) as goods_number_total from db_amzordertmpsell group by ordertmpid) as A on A.ordertmpid=B.id","$wherestr  {$date_where}
         group by dates","*,sum(B.shipping_fee)
          as shipping_fee_total,SUM(B.goods_amount) as goods_amount_total,sum(shipping_fee) as realfee,sum(payship_fee) as payship_fee_total,
          count(B.id) as order_total",'order by dates desc'.$limit);
       //echo $this->db->last_query();
        $max_date=$order_list[0]['dates'];
       // $ship_price=$this->fun->getdatabaseslist("db_amzordertmpsell A left join db_amzordertmp B on A.ordertmpid=B.id
       //     left join t_overseaproduct C on A.productid=C.sku",
       //     "$wherestr  {$date_where} group by dates","dates,sum(C.deliverprice*A.ordernum) as deliverprice_total");
        //echo $this->db->last_query();
        //dump($ship_price);
       // $deliverprices=iarray_column($ship_price,'deliverprice_total','dates');
       $costpriceData=$this->fun->getdatabaseslist("db_amzordertmp B left join db_amzordertmpsell A on A.ordertmpid=B.id",
        "$wherestr  {$date_where}","*,sum(A.costprice) as costprice_total,dates,ordertmpid,sum(ordernum) as goods_number_total");
        
        $sell_goods_list=$this->fun->getdatabaseslist("db_amzordertmp B left join db_amzordertmpsell A on A.ordertmpid=B.id",
        "$wherestr  {$date_where} group by A.productid,B.dates","dates,productid,sum(ordernum) as ordernum_total,goods_id,productname");
        $sell_skus=iarray_column($sell_goods_list,'productid');
        $sell_skus_str=implode("','",$sell_skus);
        
        $exporess_prices=$this->fun->getdatabaseslist("t_overseaproduct",
        " AND sku in('".$sell_skus_str."')","sku,deliverprice");
        $sku_shipfre=iarray_column($exporess_prices,'deliverprice','sku');
        $goods_day_firstships=array();
        foreach($sell_goods_list as $key=>$val){
            $goods_day_firstships[$val['dates']]+=$val['ordernum_total']*$sku_shipfre[$val['productid']];
            $ship_price_total+=$val['ordernum_total']*$sku_shipfre[$val['productid']];
        }
        
        
        $total_info_data =  $this->fun->getdatabaseslist("db_amzordertmp B left join (select sum(costprice) as costprice_total,ordertmpid,
        sum(ordernum) as goods_number_total from db_amzordertmpsell group by ordertmpid) as A on A.ordertmpid=B.id","$wherestr  {$date_where}",
        "*,sum(B.shipping_fee)
          as shipping_fee_total,SUM(B.goods_amount) as goods_amount_total,sum(shipping_fee) as realfee,sum(payship_fee) as payship_fee_total,
          count(B.id) as order_total",'order by dates desc');
        $costpriceData=$this->fun->getdatabaseslist("db_amzordertmp B left join db_amzordertmpsell A on A.ordertmpid=B.id",
        "$wherestr  {$date_where}","*,sum(A.costprice) as costprice_total,dates,ordertmpid,sum(ordernum) as goods_number_total");
        foreach($costpriceData as $index=>$sourceprice){
            $total_costprice+=$sourceprice['costprice_total'];
            $sell_goods_num+=$sourceprice['goods_number_total'];    
        }//left join t_overseaproduct C on A.productid=C.sku  sum(ordernum*deliverprice) as ship_price
          
        $mindate=$order_list[count($order_list)-1]['dates'];
        $date_where="and dates between '".$mindate."' and '".$max_date."' and status=1";
        $costpriceData=$this->fun->getdatabaseslist("db_amzordertmp B left join db_amzordertmpsell A on A.ordertmpid=B.id",
        "$wherestr  {$date_where} group by dates"
        ,"*,sum(costprice) as costprice_total,dates,ordertmpid,sum(ordernum) as goods_number_total");
        //echo $this->db->last_query();
      

        
        $total_info_data[0]["costprice_total"]=$total_costprice;
        $total_info_data[0]["goods_number_total"]=$sell_goods_num;
        $total_info_data[0]["ship_price_total"]=$ship_price_total;
        
        //dump($total_info_data);
        foreach($order_list as $key=>$orderinfo){
            foreach($costpriceData as $index=>$sourceprice){
                if($orderinfo['dates']==$sourceprice['dates']){
                    $order_list[$key]['costprice_total']=$sourceprice['costprice_total'];
                    $order_list[$key]['ship_price']=$goods_day_firstships[$sourceprice['dates']];
                    $order_list[$key]['goods_number_total']=$sourceprice['goods_number_total'];   
                }
            }
        }
        
        
        
        $ret['total_data']=$total_info_data;
        $ret['list']=$order_list;
        return $ret;
    }
    
    public function getProgamFBAsales($orderDate='',$wherestr='',$end_date='',$goods_arr_str=''){
        $wherestr.=" and goods_id in($goods_arr_str)";
        if($orderDate!='' or $end_date!=''){
            $date_where=$end_date!=''?"and dates between '".$orderDate."' and '".$end_date."'":"and dates='".$orderDate."'";
        }
        $date_where.=" and status=1";
        $sqlCount ="SELECT COUNT(*) as total FROM (SELECT sum(B.shipping_fee) FROM db_amzordertmp B
         left join db_amzordertmpsell A on A.ordertmpid=B.id where 1 $wherestr  {$date_where} group by dates) AA";
        $allCount = $this->db->get_one($sqlCount);
        $ret['total']=$allCount['total'];
        $order_list =  $this->fun->getdatabaseslist("db_amzordertmp B left join (select sum(costprice) as costprice_total,ordertmpid,
        sum(ordernum) as goods_number_total,goods_id from db_amzordertmpsell where goods_id in($goods_arr_str) group by ordertmpid) as A on A.ordertmpid=B.id","$wherestr  {$date_where}
         group by dates","*,sum(B.shipping_fee)
          as shipping_fee_total,SUM(B.goods_amount) as goods_amount_total,sum(shipping_fee) as realfee,sum(payship_fee) as payship_fee_total,
          count(B.id) as order_total",'order by dates desc'.$limit);
        $max_date=$order_list[0]['dates'];
        //echo $this->db->last_query();
       $costpriceData=$this->fun->getdatabaseslist("db_amzordertmp B left join db_amzordertmpsell A on A.ordertmpid=B.id",
        "$wherestr  {$date_where}","*,sum(A.costprice) as costprice_total,dates,ordertmpid,sum(ordernum) as goods_number_total");
        
        $sell_goods_list=$this->fun->getdatabaseslist("db_amzordertmp B left join db_amzordertmpsell A on A.ordertmpid=B.id",
        "$wherestr  {$date_where} group by A.productid,B.dates","dates,productid,sum(ordernum) as ordernum_total,goods_id,productname");
        $sell_skus=iarray_column($sell_goods_list,'productid');
        $sell_skus_str=implode("','",$sell_skus);
        
        $exporess_prices=$this->fun->getdatabaseslist("t_overseaproduct",
        " AND sku in('".$sell_skus_str."')","sku,deliverprice");
        $sku_shipfre=iarray_column($exporess_prices,'deliverprice','sku');
        $goods_day_firstships=array();
        foreach($sell_goods_list as $key=>$val){
            $goods_day_firstships[$val['dates']]+=$val['ordernum_total']*$sku_shipfre[$val['productid']];
            $ship_price_total+=$val['ordernum_total']*$sku_shipfre[$val['productid']];
        }
        
        
        $total_info_data =  $this->fun->getdatabaseslist("db_amzordertmp B left join (select sum(costprice) as costprice_total,ordertmpid,
        sum(ordernum) as goods_number_total,goods_id from db_amzordertmpsell where goods_id in($goods_arr_str) group by ordertmpid) as A on A.ordertmpid=B.id","$wherestr  {$date_where}",
        "*,sum(B.shipping_fee)
          as shipping_fee_total,SUM(B.goods_amount) as goods_amount_total,sum(shipping_fee) as realfee,sum(payship_fee) as payship_fee_total,
          count(B.id) as order_total",'order by dates desc');
        $costpriceData=$this->fun->getdatabaseslist("db_amzordertmp B left join db_amzordertmpsell A on A.ordertmpid=B.id",
        "$wherestr  {$date_where}","*,sum(A.costprice) as costprice_total,dates,ordertmpid,sum(ordernum) as goods_number_total");
        foreach($costpriceData as $index=>$sourceprice){
            $total_costprice+=$sourceprice['costprice_total'];
            $sell_goods_num+=$sourceprice['goods_number_total'];    
        }//left join t_overseaproduct C on A.productid=C.sku  sum(ordernum*deliverprice) as ship_price
          
        $mindate=$order_list[count($order_list)-1]['dates'];
        $date_where="and dates between '".$mindate."' and '".$max_date."'";
        $costpriceData=$this->fun->getdatabaseslist("db_amzordertmp B left join db_amzordertmpsell A on A.ordertmpid=B.id",
        "$wherestr  {$date_where} group by dates"
        ,"*,sum(costprice) as costprice_total,dates,ordertmpid,sum(ordernum) as goods_number_total");
      

        
        $total_info_data[0]["costprice_total"]=$total_costprice;
        $total_info_data[0]["goods_number_total"]=$sell_goods_num;
        $total_info_data[0]["ship_price_total"]=$ship_price_total;
        
        //dump($total_info_data);
        foreach($order_list as $key=>$orderinfo){
            foreach($costpriceData as $index=>$sourceprice){
                if($orderinfo['dates']==$sourceprice['dates']){
                    $order_list[$key]['costprice_total']=$sourceprice['costprice_total'];
                    $order_list[$key]['ship_price']=$goods_day_firstships[$sourceprice['dates']];
                    $order_list[$key]['goods_number_total']=$sourceprice['goods_number_total'];   
                }
            }
        }
        
        
        
        $ret['total_data']=$total_info_data;
        $ret['list']=$order_list;
        return $ret;
    }
    
    
	function getfreight($orderDate,$whereshop='',$end_date='')
	{
		if($whereshop)$where=" AND shopname in('".$whereshop."')";
		$nums = 0;
        $date_where=$end_date!=''?"dates between '".$orderDate."' and '".$end_date."'":"dates='".$orderDate."'";
		$order = $this->db->select('shipping_expectfee,shipping_realfee')
        ->get_where('db_order_list' ," {$date_where} AND order_status !=6 AND order_status !=7 $where")->result_array();
		foreach($order as $skey=>$res)
		{
			if($res['shipping_realfee']>0)
			{
				$nums = $nums+$res['shipping_realfee'];
			}else{
				$nums = $nums+$res['shipping_expectfee'];
			}
		}
		return $nums;
	}
	function getplatformfee($orderDate,$whereshop='',$end_date='')
	{
		if($whereshop)$where=" AND shopname in('".$whereshop."')";
        $date_where=$end_date!=''?"dates between '".$orderDate."' and '".$end_date."'":"dates='".$orderDate."'";
        
		$order = $this->fun->getdatabasesdetail('db_order_list'," AND {$date_where} AND order_status !=6 AND order_status !=7 $where",' SUM(platform_fee) as Num');
		$payfee = $this->fun->getdatabasesdetail('db_order_list'," AND {$date_where} AND order_status !=6 AND order_status !=7 $where",' SUM(pay_fee) as Num');
		return array("platform"=>$order['Num'],"payfee"=>$payfee['Num']);
	}
	function getgoodsPublishAnalysis($sku,$createUser,$noupsku,$nums)
	{
		
		
		$datalist = $this->fun->getdatabaseslist('db_product_develop_addtime A LEFT JOIN db_product_develop B on A.sku = B.sku ' , " AND A.sku in('{$sku}') AND A.createUser = '".$createUser."' ","UNIX_TIMESTAMP(addTime) as addTime,UNIX_TIMESTAMP(linkUrl_addtime) as linkUrl_addtime,A.sku","group by A.sku");
		if($datalist)
		{
			$datanum = $nodatanum = 0;
			foreach($datalist as $key =>$rslist)
			{
				$isdatanum = $rslist['linkUrl_addtime'] - $rslist['addTime'];
				$datanum = $datanum+$isdatanum;
			}

			//计算未上架的SKU
			
			
		}

		if($noupsku)
		{
			$lastnoupsku= "";
			$lastnoupsku = explode(",",$noupsku);
			$noupsku = implode("','",$lastnoupsku);
			$nodatalist = $this->fun->getdatabaseslist("db_product_develop"," AND sku in('{$noupsku}')",'UNIX_TIMESTAMP(linkUrl_addtime) as linkUrl_addtime');
			
			if($nodatalist){
				
				foreach($nodatalist as $key =>$rslist)
				{
					$isdatanum = $rslist['linkUrl_addtime'] - time();//未上架的时间都计算到当前
					$nodatanum = $nodatanum+$isdatanum;
				}
			}
			
		}

		//未上新的需要更新到当前时间
		$datanums = abs($datanum)+abs($nodatanum);
		if($datanums>0){
			$datanum  = round($datanums / 86400);
			return round($datanum/$nums,2);
		}else{
			return 0;
		}
	}
}

/* End of file Report_m.php */